//
//  GXServiceViewController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/11/28.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXServiceViewController.h"

@interface GXServiceViewController ()
@property (nonatomic,strong) NSMutableArray * selectedMarkStrArray;
//@property (nonatomic,strong) NSMutableArray * serviceArray;
@property (nonatomic,copy) NSString *serviceStr;//记录请求的服务项
@property (nonatomic,copy) NSString *changeServiceStr;//记录选择后的服务项
@property (nonatomic,strong) NSDictionary * wordKeyDic;
@property (nonatomic,strong) NSDictionary * numberKeyDic;
@end

@implementation GXServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"服务";
    [self setUpServiceValue];
}
- (void)setUpServiceValue
{
    [ShopAuthenticationManager selectShopServiceWithStoreID:StoreId success:^(id responseObject) {
        if (responseObject) {
            self.serviceStr = [NSString stringWithFormat:@"%@",responseObject[@"describa"]];
            [self handleRequestServiceData:self.serviceStr];
        }
    } fail:^(id responseObject) {
        
    }];
}
- (void)handleRequestServiceData:(NSString *)str
{
    if (![NSString isBlankString:str]) {
        NSArray *array = [str  componentsSeparatedByString:@"-"];
        for (int i =0; i<array.count; i++) {
            NSInteger j = [[self.wordKeyDic objectForKey:array[i]] integerValue];
            UIButton * serviceButton = [self.view viewWithTag:j];
            serviceButton.selected = YES;
        }
        [self.selectedMarkStrArray addObjectsFromArray:array];
    }
}
- (NSDictionary *)numberKeyDic
{
    if (!_numberKeyDic) {
        _numberKeyDic = [NSDictionary dictionary];
        _numberKeyDic= @{
                 @"61" : @"A",
                 @"62" : @"B",
                 @"63" : @"C",
                 @"64" : @"D",
                 @"65" : @"E",
                 @"66" : @"F" ,
                 @"67" : @"G" ,
                 @"68" : @"H" ,
                 @"69" : @"I" ,
                 @"70" : @"J" ,
                 @"71" : @"K" ,
                 };
    }
    return _numberKeyDic;
}
- (NSDictionary *)wordKeyDic
{
    if (!_wordKeyDic) {
        _wordKeyDic = [NSDictionary dictionary];
        _wordKeyDic = @{
                              @"A" : @"61",
                              @"B" : @"62",
                              @"C" : @"63",
                              @"D" : @"64",
                              @"E" : @"65",
                              @"F" : @"66" ,
                              @"G" : @"67" ,
                              @"H" : @"68" ,
                              @"I" : @"69" ,
                              @"J" : @"70" ,
                              @"K" : @"71" ,
                              };
    }
    return _wordKeyDic;
}

- (NSMutableArray *)selectedMarkStrArray {
    if (!_selectedMarkStrArray) {
        _selectedMarkStrArray = [NSMutableArray array];
    }
    return _selectedMarkStrArray;
}
- (IBAction)clickItemButton:(UIButton *)sender {
    sender.selected = ! sender.selected;
    NSString *tagStr = [NSString stringWithFormat:@"%ld",sender.tag];
    if (sender.selected) {
        [self.selectedMarkStrArray addObject:[self.numberKeyDic objectForKey:tagStr]];
    }else
    {
        [self.selectedMarkStrArray removeObject:[self.numberKeyDic objectForKey:tagStr]];
    }
    NSLog(@"self.selectedMarkStrArray%@",self.selectedMarkStrArray);
}

- (IBAction)clickSaveBtn:(UIButton *)sender {
    if (![self judgeChangeServiceItem]) {
        return;
    }
    [ShopAuthenticationManager changeShopServiceWithStoreID:StoreId serviceSige:self.changeServiceStr success:^(id responseObject) {
        if (responseObject) {
            if (self.changeServiceBlock) {
                self.changeServiceBlock(YES);
            }
        }
    } fail:^(id responseObject) {
    }];
}
- (BOOL)judgeChangeServiceItem
{
    if (!self.selectedMarkStrArray.count) {
        [SVProgressHUD showErrorWithStatus:@"服务项不能为空"];
        return NO;
    }
        self.changeServiceStr = [self.selectedMarkStrArray componentsJoinedByString:@"-"];
    if ([self.serviceStr isEqualToString:self.changeServiceStr]) {
         [SVProgressHUD showErrorWithStatus:@"服务项未做修改"];

        return NO;
    }
    return YES;
}
@end
